package edu.servletcontext;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * 4种获取ServletContext的方式
 */
@WebServlet("/servletContext01")
public class ServletContextDemo01 extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //通过this直接获取
        ServletContext servletContext = this.getServletContext();
        System.out.println(servletContext);

        //通过ServletConfig对象获取
        servletContext = this.getServletConfig().getServletContext();
        System.out.println(servletContext);

        //通过request对象获取
        servletContext = request.getServletContext();
        System.out.println(servletContext);

        //通过HttpSession获取
        servletContext = request.getSession().getServletContext();
        System.out.println(servletContext);

        System.out.println(servletContext.getAttribute("AttrTest"));
    }
}
